layui.use(['form', 'layer'], function () {
  // 获取form实例
  var form = layui.form;
  var layer = layui.layer
  // 监听某个表单的提交事件 必须要给登录按钮添加 lay-filter="loginform"属性
  // data表示的是表单里面提交的数据，比如用户名和密码
  form.on('submit(loginform)', function (data) {
    login(data.field)
      .then(res => {
        console.log(res);
        let {data: {message, code, token}} = res
        if(code ===  0) {
          layer.msg(message)
          // 跳转到首页
          localStorage.setItem('token', token)
          setTimeout(() => { location.href = '/index.html' }, 1000)
        } else {
          layer.msg(message)
        }
      })
    return false;
  });
})

let flag='true';
 let linkreg=document.querySelector('#link_reg');
 let loginbox=document.querySelector('.login-box');
 let linklogin=document.querySelector('#link_login');
 let user=document.querySelector('.username');
 let password=document.querySelector('.password');
 let repassword = document.querySelector('.repassword');
// 获取登录按钮
let reg=document.querySelector('.reg-box');
let regbtn=document.querySelector('.regbtn');
let register=(obj)=>axios.post('/api/reg',obj);

console.log(reg);
// 去注册
linkreg.addEventListener('click',async function () {
  console.log(repassword);
    reg.style.display='block';
    loginbox.style.display='none';
 
})
// 注册
regbtn.addEventListener('click',async function () {
  let obj={
    "username": user.value,
    "password": password.value,
    "repassword": repassword.value
  };
 let res= await register(obj);
 if (res.data.code===0) {
  reg.style.display='none';
  loginbox.style.display='block';
  alert(res.data.message);
 }else{
  alert(res.data.message);
 }
  console.log(res.data);
})
// 去登录
linklogin.addEventListener('click',function () {
  reg.style.display='none';
  loginbox.style.display='block';
})

